Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / ui / src / commonMain / kotlin / org / meshtastic / core / ui / util / ProtoExtensions.kt

Displaying Raw • Download

core/ui/src/commonMain/kotlin/org/meshtastic/core/ui/util/ProtoExtensions.kt 1fdc9214896723f4be94aaa5c1d8527d2d1b04fa (1fdc9214) Text, 9.77 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ui.util

Tff7b72import T7ee787androidx.compose.runtime.Composable
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.NonCancellable
Tff7b72import T7ee787kotlinx.coroutines.flow.first
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787org.jetbrains.compose.resources.stringResource
Tff7b72import T7ee787org.meshtastic.core.common.util.DateFormatter
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis
Tff7b72import T7ee787org.meshtastic.core.model.util.channelIdentity
Tff7b72import T7ee787org.meshtastic.core.model.util.isChannelPlaceholder
Tff7b72import T7ee787org.meshtastic.core.model.util.toChannelReplacementPlan
Tff7b72import T7ee787org.meshtastic.core.repository.RadioConfigRepository
Tff7b72import T7ee787org.meshtastic.core.repository.RadioController
Tff7b72import T7ee787org.meshtastic.core.resources.Res
Tff7b72import T7ee787org.meshtastic.core.resources.unknown_age
Tff7b72import T7ee787org.meshtastic.proto.Channel
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.Config
Tff7b72import T7ee787org.meshtastic.proto.MeshPacket
Tff7b72import T7ee787org.meshtastic.proto.Position
Tff7b72import T7ee787kotlin.time.Duration.Companion.days

Tff7b72private Tff7b72const Tff7b72val Te6edf3SECONDS_TO_MILLIS Tff7b72= T79c0ff1T79c0ff0T79c0ff0T79c0ff0L

Tf0883e@Composable
Tff7b72fun Te6edf3PositionTb4b4b4.Td2a8ffformatPositionTimeTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657String Tb4b4b4{
Tff7b72val Te6edf3currentTime Tff7b72= Te6edf3nowMillis
Tff7b72val Te6edf3sixMonthsAgo Tff7b72= Te6edf3currentTime Tff7b72- T79c0ff1T79c0ff8T79c0ff0.Te6edf3daysTb4b4b4.Te6edf3inWholeMilliseconds
Tff7b72val Te6edf3isOlderThanSixMonths Tff7b72= Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLIS Tff7b72< Te6edf3sixMonthsAgo
Tff7b72val Te6edf3timeText Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3isOlderThanSixMonthsTb4b4b4) Tb4b4b4{
Te6edf3stringResourceTb4b4b4(Te6edf3ResTb4b4b4.Te6edf3stringTb4b4b4.Te6edf3unknown_ageTb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3DateFormatterTb4b4b4.Te6edf3formatDateTimeTb4b4b4(Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLISTb4b4b4)
Tb4b4b4}
Tff7b72return Te6edf3timeText
Tb4b4b4}

Tff7b72fun Te6edf3MeshPacketTb4b4b4.Td2a8fftoPositionTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Position? Tb4b4b4{
Tff7b72val Te6edf3decoded Tff7b72= Te6edf3decoded Tff7b72?: Tff7b72return Tff7b72null
Tff7b72return Tff7b72if Tb4b4b4(Te6edf3decodedTb4b4b4.Te6edf3want_response Tff7b72!Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3decodedTb4b4b4.Te6edf3payloadTb4b4b4.Te6edf3let Tb4b4b4{ Te6edf3runCatching Tb4b4b4{ Te6edf3PositionTb4b4b4.Te6edf3ADAPTERTb4b4b4.Te6edf3decodeTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Builds a [Channel] list from the difference between two [ChannelSettings] lists. Only changes are included in the
* resulting list.
*
* @param new The updated [ChannelSettings] list.
* @param old The current [ChannelSettings] list (required when disabling unused channels).
* @return A [Channel] list containing only the modified channels.
*/
Tff7b72fun Td2a8ffgetChannelListTb4b4b4(Te6edf3newTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Te6edf3oldTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelTff7b72> Tff7b72= Te6edf3buildList Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3i Tff7b72in T79c0ff0.Tb4b4b4.Te6edf3maxOfTb4b4b4(Te6edf3oldTb4b4b4.Te6edf3lastIndexTb4b4b4, Te6edf3newTb4b4b4.Te6edf3lastIndexTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3oldTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72!Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(
Te6edf3ChannelTb4b4b4(
Te6edf3role Tff7b72=
Tff7b72when Tb4b4b4(Te6edf3iTb4b4b4) Tb4b4b4{
T79c0ff0 Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3PRIMARY
Tff7b72in T79c0ff1.Tb4b4b4.Te6edf3newTb4b4b4.Te6edf3lastIndex Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3SECONDARY
Tff7b72else Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED
Tb4b4b4}Tb4b4b4,
Te6edf3index Tff7b72= Te6edf3iTb4b4b4,
Te6edf3settings Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Imports a [ChannelSet] as an authoritative REPLACE: writes every channel and — when present and actually different —
* the imported LoRa config, all inside one [RadioController.editLocalSettings] transaction, then replaces the local
* channel cache.
*
* Reads the current LoRa config and channel set from [radioConfigRepository]'s flows (avoiding the StateFlow
* placeholder window) and builds the shared authoritative replacement plan. The edit-settings transaction defers disk
* persistence, radio reload/reconfiguration, and reboot until the closing commit, so channels + LoRa land in a single
* reboot with no per-slot reconfigure to pace against. (Firmware still writes each `set_channel` into its in-memory
* channel table as it arrives — the transaction is not a full staging of channel state — but the expensive
* persist/reload path runs once at commit.) Writing LoRa inside the same session mirrors `InstallProfileUseCase` and is
* why the old pre/post settle delays are gone: the begin/commit boundary is the settle.
*
* The local channel cache is commit-shaped: transactional channel writes deliberately do not mirror per slot (see
* `AdminControllerImpl.EditSettingsSession.setChannel`), and this function replaces the cached channel list once, after
* the session succeeds — so an import interrupted before that point leaves the local channel cache untouched. The
* post-commit [RadioConfigRepository.updateChannelSet] call updates the normalized settings and imported LoRa config
* together; an import without LoRa preserves the cached LoRa config.
*
* Imported settings are normalized before any write or bounds check, so blank placeholder secondaries and semantic
* duplicates never reach the radio or the local cache.
*
* @param channelSet The imported [ChannelSet] to apply as a replacement. Its `lora_config`, if present and different
* from the device's current LoRa config, is written inside the same transaction.
* @param radioController The [RadioController] used to run the edit transaction.
* @param radioConfigRepository The [RadioConfigRepository] providing the current channel/LoRa flows and cache.
*/
Tff7b72suspend Tff7b72fun Td2a8ffimportChannelSetTb4b4b4(
Te6edf3channelSetTb4b4b4: Te6edf3ChannelSetTb4b4b4,
Te6edf3radioControllerTb4b4b4: Te6edf3RadioControllerTb4b4b4,
Te6edf3radioConfigRepositoryTb4b4b4: Te6edf3RadioConfigRepositoryTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3currentLoraConfig Tff7b72= Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3localConfigFlowTb4b4b4.Te6edf3firstTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3lora
Tff7b72val Te6edf3currentSettings Tff7b72= Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3channelSetFlowTb4b4b4.Te6edf3firstTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3settings
Tff7b72val Te6edf3replacementPlan Tff7b72=
Te6edf3channelSetTb4b4b4.Te6edf3toChannelReplacementPlanTb4b4b4(Te6edf3currentSettings Tff7b72= Te6edf3currentSettingsTb4b4b4, Te6edf3fallbackLoraConfig Tff7b72= Te6edf3currentLoraConfigTb4b4b4)
T8b949e// Only write LoRa when the import carries one that actually differs from the device — avoids a redundant
T8b949e// reconfigure.
Tff7b72val Te6edf3importedLoraConfig Tff7b72= Te6edf3channelSetTb4b4b4.Te6edf3lora_configTff7b72?.Te6edf3takeIf Tb4b4b4{ Tffa657it Tff7b72!Tff7b72= Te6edf3currentLoraConfig Tb4b4b4}
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{
Ta5d6ff"Ta5d6ffApplying imported channel replacement writes=Tffd700${Te6edf3replacementPlanTb4b4b4.Te6edf3channelWritesTb4b4b4.Te6edf3sizeTffd700}Ta5d6ff Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6ffimportedSettings=Tffd700${Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4.Te6edf3sizeTffd700}Ta5d6ff Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6ffnormalizedSettings=Tffd700${Te6edf3replacementPlanTb4b4b4.Te6edf3normalizedSettingsTb4b4b4.Te6edf3sizeTffd700}Ta5d6ff Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6ffwritesLora=Tffd700${Te6edf3importedLoraConfig Tff7b72!Tff7b72= Tff7b72nullTffd700}Ta5d6ff"
Tb4b4b4}
Te6edf3radioControllerTb4b4b4.Te6edf3editLocalSettings Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3channel Tff7b72in Te6edf3replacementPlanTb4b4b4.Te6edf3channelWritesTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{
Ta5d6ff"Ta5d6ffWriting imported channel index=Tffd700${Te6edf3channelTb4b4b4.Te6edf3indexTffd700}Ta5d6ff role=Tffd700${Te6edf3channelTb4b4b4.Te6edf3roleTffd700}Ta5d6ff Ta5d6ff" Tff7b72+
Ta5d6ff"Ta5d6ffhasName=Tffd700${Te6edf3channelTb4b4b4.Te6edf3settingsTff7b72?.Te6edf3nameTff7b72?.Te6edf3isNotBlankTb4b4b4(Tb4b4b4) Tff7b72=Tff7b72= Tff7b72trueTffd700}Ta5d6ff"
Tb4b4b4}
Te6edf3setChannelTb4b4b4(Te6edf3channelTb4b4b4)
Tb4b4b4}
Te6edf3importedLoraConfigTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3setConfigTb4b4b4(Te6edf3ConfigTb4b4b4(Te6edf3lora Tff7b72= Tffa657itTb4b4b4)Tb4b4b4) Tb4b4b4}
Tb4b4b4}
Te6edf3withContextTb4b4b4(Te6edf3NonCancellableTb4b4b4) Tb4b4b4{
Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3updateChannelSetTb4b4b4(
Te6edf3settingsList Tff7b72= Te6edf3replacementPlanTb4b4b4.Te6edf3normalizedSettingsTb4b4b4,
Te6edf3loraConfig Tff7b72= Te6edf3importedLoraConfigTb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Builds the filtered ADD-mode preview for QR import: existing channels followed by only the unique incoming channels.
*
* Incoming channels that are semantic duplicates (same effective name + effective PSK) of an existing or earlier
* incoming channel are omitted from the preview. Unique incoming channels are appended in scanned order and selected by
* default while firmware channel capacity remains; unique channels beyond [maxChannels] stay visible but unchecked.
*
* Semantic identity is resolved via the [Channel] domain model so preset/default channels match correctly across modem
* presets: empty names resolve to the preset display name, and 1-byte PSK markers expand to the full default key.
*
* @param existing The current [ChannelSettings] list on the radio. Always shown, always selected.
* @param incoming The imported [ChannelSettings] list. Duplicates omitted; uniques appended in order.
* @param loraConfig The current [Config.LoRaConfig], used to resolve effective channel identity.
* @param maxChannels Firmware channel limit. Unique incoming selections stop when this is reached.
* @return A [ChannelAddPreview] whose [settings] and [selections] are aligned and size-matched.
*/
Tff7b72fun Td2a8ffgetChannelPreviewForAddTb4b4b4(
Te6edf3existingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Te6edf3incomingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Te6edf3loraConfigTb4b4b4: Te6edf3ConfigTb4b4b4.Te6edf3LoRaConfigTb4b4b4,
Te6edf3maxChannelsTb4b4b4: Tffa657IntTb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3ChannelAddPreview Tb4b4b4{
Tff7b72val Te6edf3seen Tff7b72= Te6edf3existingTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3channelIdentityTb4b4b4(Te6edf3loraConfigTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3toMutableSetTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3previewSettings Tff7b72= Te6edf3existingTb4b4b4.Te6edf3toMutableListTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3previewSelections Tff7b72= Te6edf3MutableListTb4b4b4(Te6edf3existingTb4b4b4.Te6edf3sizeTb4b4b4) Tb4b4b4{ Tff7b72true Tb4b4b4}
Tff7b72var Te6edf3remaining Tff7b72= Tb4b4b4(Te6edf3maxChannels Tff7b72- Te6edf3existingTb4b4b4.Te6edf3sizeTb4b4b4)Tb4b4b4.Te6edf3coerceAtLeastTb4b4b4(T79c0ff0Tb4b4b4)
Tff7b72for Tb4b4b4(Te6edf3channel Tff7b72in Te6edf3incomingTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3shouldShow Tff7b72= Tff7b72!Te6edf3channelTb4b4b4.Te6edf3isChannelPlaceholderTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3identity Tff7b72= Tff7b72if Tb4b4b4(Te6edf3shouldShowTb4b4b4) Te6edf3channelTb4b4b4.Te6edf3channelIdentityTb4b4b4(Te6edf3loraConfigTb4b4b4) Tff7b72else Tff7b72null
T8b949e// Omit blank placeholders and semantic duplicates entirely — they are not shown to the user.
Tff7b72if Tb4b4b4(Te6edf3identity Tff7b72!Tff7b72= Tff7b72null Tff7b72&Tff7b72& Te6edf3seenTb4b4b4.Te6edf3addTb4b4b4(Te6edf3identityTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3previewSettings Tff7b72+Tff7b72= Te6edf3channel
Tff7b72val Te6edf3shouldSelect Tff7b72= Te6edf3remaining Tff7b72> T79c0ff0
Te6edf3previewSelections Tff7b72+Tff7b72= Te6edf3shouldSelect
Tff7b72if Tb4b4b4(Te6edf3shouldSelectTb4b4b4) Te6edf3remainingTff7b72-Tff7b72-
Tb4b4b4}
Tb4b4b4}
Tff7b72return Te6edf3ChannelAddPreviewTb4b4b4(Te6edf3settings Tff7b72= Te6edf3previewSettingsTb4b4b4, Te6edf3selections Tff7b72= Te6edf3previewSelectionsTb4b4b4)
Tb4b4b4}

T8b949e/** Filtered ADD-mode preview: the visible channel list paired with its default selections (always size-matched). */
Tff7b72data Tff7b72class T56d364ChannelAddPreviewTb4b4b4(Tff7b72val Te6edf3settingsTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Tff7b72val Te6edf3selectionsTb4b4b4: Te6edf3ListTff7b72<Tffa657BooleanTff7b72>Tb4b4b4)

Served by rngit 1.5.0 - Generated in 0.06s